package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.system.entity.Payment;
import org.example.system.mapper.PaymentMapper;
import org.example.system.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 缴费服务层实现类
 */
@Service
public class PaymentServiceImpl implements PaymentService {
    @Autowired
    private PaymentMapper paymentMapper;

    /**
     * 按条件分页查询
     * @param payment
     * @return
     */
    @Override
    public IPage<Payment> selectPage(Payment payment){
        LambdaQueryWrapper<Payment> wrapper = new LambdaQueryWrapper<>();
        //条件
        if (StrUtil.isNotEmpty(payment.getAddress())){
            wrapper.eq(Payment:: getAddress,payment.getAddress());    //相同的缴费地址（精确查询）
        }
        IPage<Payment> page = new Page<>();
        page.setCurrent(payment.getPageNum());
        page.setSize(payment.getPageSize());
        return paymentMapper.selectPage(page,wrapper);
    }

    /**
     * 添加缴费
     * @param payment
     * @return
     */
    @Override
    public int add(Payment payment) {
        payment.setCreateTime(new Date());
        return paymentMapper.insert(payment);
    }

    /**
     * 修改
     * @param payment
     * @return
     */
    @Override
    public int update(Payment payment) {
        payment.setUpdateTime(new Date());
        return paymentMapper.updateById(payment);
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @Override
    public Payment getOne(Long id) {
        return paymentMapper.selectById(id);
    }
}
